package com.lbs.jiepiaisi.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lbs.jiepiaisi.R;

/**
 * Created by yu on 2020-06-12 15:47.
 * Description:
 */
public class UploadProgressDialog extends Dialog {

    private TextView tipView;
    private ProgressBar progressBar;
    private ProgressBar zipBar;
    private TextView progressView;
    private ImageView successView;



    public UploadProgressDialog(@NonNull Context context) {
        super(context);
    }

    public UploadProgressDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected UploadProgressDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setCancelable(false);
        setCanceledOnTouchOutside(false);

        setContentView(R.layout.upload_progress_dialog);
//        WindowManager.LayoutParams params = getWindow().getAttributes();
//        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
//        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
//        getWindow().setAttributes(params);

        initView();
    }

    private void initView(){
        tipView = findViewById(R.id.tv_tip);
        successView = findViewById(R.id.iv_success);
        progressBar = findViewById(R.id.pb_progress);
        zipBar = findViewById(R.id.pb_zip);
        progressView = findViewById(R.id.tv_show_progress);
    }

    public void setTip(String tip) {
        if (tip != null) {
            this.tipView.setText(tip);
            if (tip.contains("压缩")) {
                this.zipBar.setVisibility(View.VISIBLE);
                this.progressBar.setVisibility(View.GONE);
                this.progressView.setVisibility(View.GONE);
            } else {
                this.zipBar.setVisibility(View.GONE);
                this.progressBar.setVisibility(View.VISIBLE);
                this.progressView.setVisibility(View.VISIBLE);
            }
        }
    }

    @SuppressLint("SetTextI18n")
    public void setProgress(int progress) {
        if (progress <= 100) {
            this.progressBar.setProgress(progress);
            this.progressView.setText(progress + "%");
        }

        if (progress == 100) {
            setTip("图片上传成功");
            this.progressBar.setVisibility(View.GONE);
            this.progressView.setVisibility(View.GONE);
            this.successView.setVisibility(View.VISIBLE);
        } else {
            setTip("正在上传图片");
            this.progressBar.setVisibility(View.VISIBLE);
            this.progressView.setVisibility(View.VISIBLE);
            this.successView.setVisibility(View.GONE);
        }
    }

}
